package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        request.setCharacterEncoding("utf-8");
        //     获取请求的URI
        String URI = request.getRequestURI() ;
        //    排除静态页面
        if (URI.contains(".css") || URI.contains(".png")||URI.contains(".jpg")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        servletResponse.setContentType("text/html,charset=utf-8");
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
